from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

from ..utils.choices import IMAGE_TYPE
from . import services


class DefectType(models.Model):
    """
    缺陷类型
    """
    name = models.CharField(max_length=128, null=False, blank=True, verbose_name=_("name"))
    "修复视频"
    video = models.FileField(upload_to=services.get_repair_video_path(), null=True, blank=True, verbose_name=_("defect video"))
    "修复建议"
    advice = models.TextField(null=False, blank=True, verbose_name=_("defect advice"))
    "缺陷样例"
    image = models.FileField(upload_to=services.get_defect_example_image_path(), null=True, blank=True, verbose_name=_("defect image"))
    "缺陷类型"
    type = models.CharField(max_length=16, choices=IMAGE_TYPE, verbose_name=_("image type"))
    "描述"
    description = models.TextField(null=True, blank=False, verbose_name=_("description"))

    class Meta:
        verbose_name = "缺陷类型"
        verbose_name_plural = "缺陷类型"

    def __str__(self):
        return self.name


class Defect(models.Model):
    """
    缺陷
    """
    "项目"
    project = models.ForeignKey("projects.Project", null=False, blank=True, on_delete=models.CASCADE, verbose_name=_("project"))
    "缺陷类型"
    defect = models.ForeignKey("checks.DefectType", related_name="defect_type", null=False, blank=True, on_delete=models.CASCADE, verbose_name=_("defcet type"))
    "位置信息"
    location = models.CharField(max_length=64, null=False, blank=True, verbose_name=_("location"))
    "拍摄时间"
    shoot_date = models.DateTimeField(null=False, blank=True, verbose_name=_("shoot date"))
    "热成像图片路径"
    thermal = models.FileField(upload_to=services.get_defect_thermal_image_path(), null=False, blank=False, verbose_name=_("thermal image"))
    "可见光图片路径"
    visible = models.FileField(upload_to=services.get_defect_thermal_image_path(), null=False, blank=False, verbose_name=_("visible image"))
    "缺陷图片"
    defect_image = models.FileField(upload_to=services.get_defect_image_path(), null=False, blank=False, verbose_name=_("visible image"))
    "依赖模型"
    model_info = models.ForeignKey("trains.ModelInfo", null=False, blank=False, on_delete=models.CASCADE, verbose_name=_("model info"))
    "创建时间"
    created_date = models.DateTimeField(default=timezone.now, verbose_name=_("create date"))

    class Meta:
        verbose_name = "缺陷信息"
        verbose_name_plural = "缺陷信息"


class CheckConfig(models.Model):
    """
    检测配置
    """
    name = models.CharField(max_length=128, null=False, blank=True, verbose_name=_("name"))
    "输出路径"
    output = models.CharField(max_length=128, null=False, blank=True, verbose_name=_("output path"))
    "图片尺寸"
    img_size = models.PositiveIntegerField(default=640, verbose_name=_("image size"))
    "对象阈值"
    conf_thres = models.FloatField(default=0.4, verbose_name=_("confidence threshold"))
    "NMS阈值"
    iou_thres = models.FloatField(default=0.5, verbose_name=_("NMS threshold"))
    "依赖模型"
    model_info = models.ForeignKey("trains.ModelInfo", null=False, blank=False, on_delete=models.CASCADE, verbose_name=_("model info"))

    class Meta:
        verbose_name = "检测配置"
        verbose_name_plural = "检测配置"

    def __str__(self):
        return self.name


class CheckTask(models.Model):
    """
    检测任务
    """
    "项目"
    project = models.ForeignKey("projects.Project", null=False, blank=True, on_delete=models.CASCADE, verbose_name=_("project"))
    "创建时间"
    created_date = models.DateTimeField(default=timezone.now, verbose_name=_("create date"))
